/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于jv-rover系统.
 */
package com.pitop.device.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.DevicePoolException;
import com.pitop.device.dao.DevicePoolDAO;
import com.pitop.device.entity.DevicePoolState;
import com.pitop.device.entity.DevicePool;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.Map;
import org.apache.ibatis.session.RowBounds;
import com.baidu.fsg.uid.UidGenerator;

@Service("devicePoolSV")
@Slf4j
public class DevicePoolSVImpl extends BaseSVImpl<DevicePool, Long> implements DevicePoolSV {

    @Autowired
    private DevicePoolDAO devicePoolDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return devicePoolDAO;
    }

    /**
    * 保存account对象
    *
    * @param entity 实体
    * @throws BaseException
    */
    @Override
    public void save(DevicePool entity) throws BaseException {
        entity.setCode(String.valueOf(uidGenerator.getUID()));
        entity.setCodeName(String.valueOf(uidGenerator.getUID()));
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        super.save(entity);
    }

    /**
     * 加载一个对象DevicePool
     * @param id * @param code 设备编码* @param codeName 设备编号
     * @return DevicePool
     */
    @Override
    public DevicePool load(java.lang.Long id,java.lang.String code,java.lang.String codeName) {
           if(id==null&&code==null&&codeName==null){
                 throw new DevicePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
           }

            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("code",code);
            param.put("codeName",codeName);
            return devicePoolDAO.load(param);
    }
    /**
     * 加载一个对象DevicePool 通过id
     * @param id 
     * @return DevicePool
     */
    @Override
    public DevicePool loadById(java.lang.Long id) {
            if(id==null){
                 throw new DevicePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return devicePoolDAO.loadById(id);
    }

    /**
     * 加载一个对象DevicePool 通过code
     * @param code 设备编码
     * @return DevicePool
     */
    @Override
    public DevicePool loadByCode(java.lang.String code) {
            if(code==null){
                 throw new DevicePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return devicePoolDAO.loadByCode(code);
    }

    /**
     * 加载一个对象DevicePool 通过codeName
     * @param codeName 设备编号
     * @return DevicePool
     */
    @Override
    public DevicePool loadByCodeName(java.lang.String codeName) {
            if(codeName==null){
                 throw new DevicePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return devicePoolDAO.loadByCodeName(codeName);
    }


    /**
    * 根据主键id,oldStates 共同更新 DevicePool 的状态到newState状态
    *
    * @param id 
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateById(java.lang.Long id,DevicePoolState newState,DevicePoolState... oldStates){
        if(id==null){
            throw new DevicePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new DevicePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new DevicePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        devicePoolDAO.updateStateById(id,new Date(),newState,oldStates);
    }
    /**
    * 根据主键code,oldStates 共同更新 DevicePool 的状态到newState状态
    *
    * @param code 设备编码
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateByCode(java.lang.String code,DevicePoolState newState,DevicePoolState... oldStates){
        if(code==null){
            throw new DevicePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new DevicePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new DevicePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        devicePoolDAO.updateStateByCode(code,new Date(),newState,oldStates);
    }
    /**
    * 根据主键codeName,oldStates 共同更新 DevicePool 的状态到newState状态
    *
    * @param codeName 设备编号
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateByCodeName(java.lang.String codeName,DevicePoolState newState,DevicePoolState... oldStates){
        if(codeName==null){
            throw new DevicePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new DevicePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new DevicePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        devicePoolDAO.updateStateByCodeName(codeName,new Date(),newState,oldStates);
    }

    /**
    * 根据主键id 更新 DevicePool 的状态到另一个状态
    *
    * @param id 
    * @param state 状态
    */
    @Override
    public void updateById(java.lang.Long id,DevicePoolState state){
            if(id==null){
               throw new DevicePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          devicePoolDAO.updateById(id,state,new Date());
    }
    /**
    * 根据主键code 更新 DevicePool 的状态到另一个状态
    *
    * @param code 设备编码
    * @param state 状态
    */
    @Override
    public void updateByCode(java.lang.String code,DevicePoolState state){
            if(code==null){
               throw new DevicePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          devicePoolDAO.updateByCode(code,state,new Date());
    }
    /**
    * 根据主键codeName 更新 DevicePool 的状态到另一个状态
    *
    * @param codeName 设备编号
    * @param state 状态
    */
    @Override
    public void updateByCodeName(java.lang.String codeName,DevicePoolState state){
            if(codeName==null){
               throw new DevicePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          devicePoolDAO.updateByCodeName(codeName,state,new Date());
    }


    /**
     * 删除对象DevicePool
     * @param id * @param code 设备编码* @param codeName 设备编号
     * @return DevicePool
     */
    @Override
    public void delete(java.lang.Long id,java.lang.String code,java.lang.String codeName) {
            if(id==null&&code==null&&codeName==null){
                throw new DevicePoolException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("code",code);
            param.put("codeName",codeName);
            devicePoolDAO.delete(param);
    }



    /**
    * 查询DevicePool分页
    *
    * @param devicePool  对象
    * @param offset 查询开始行
    * @param limit  查询行数
    * @return List<DevicePool>
    */
    @Override
    public List<DevicePool> list(DevicePool devicePool, int offset, int limit) {
            if (offset < 0) {
               offset = 0;
            }

            if (limit < 0) {
               limit = Page.limit;
            }

            Map<String, Object> map = null;
            if (devicePool != null) {
               map = JSON.parseObject(JSON.toJSONString(devicePool, SerializerFeature.WriteDateUseDateFormat));
            } else {
               map = new HashMap<>();
            }
        return devicePoolDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public  int count(DevicePool devicePool){
        Map<String, Object> map = null;
        if (devicePool != null) {
           map = JSON.parseObject(JSON.toJSONString(devicePool, SerializerFeature.WriteDateUseDateFormat));
        } else {
           map = new HashMap<>();
        }
       return devicePoolDAO.count(map);
     }

    /**
     * 查询DevicePool分页
     *
     * @param id  * @param code  设备编码* @param codeName  设备编号
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<DevicePool>
     */
    @Override
    public List<DevicePool> list(java.lang.Long id,java.lang.String code,java.lang.String codeName,int offset, int limit) {
            if (offset < 0) {
              offset = 0;
            }

            if (limit < 0) {
              limit = Page.limit;
            }
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("code",code);
            param.put("codeName",codeName);
            return devicePoolDAO.list(param,new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id,java.lang.String code,java.lang.String codeName) {
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("code",code);
            param.put("codeName",codeName);
            return devicePoolDAO.count(param);
    }

}
